package com.clear.honghefinance.common.util.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * <p>
 * Redis配资类
 * </p>
 * ConditionalOnProperty 注解
 * 根据配置文件中的属性值来激活配置类或Bean。
 * - prefix 属性的前缀，与name一起构成完整的属性路径
 * - name 属性名，与prefix一起构成完整的属性路径
 * - havingValue 属性期望的值，只有当属性的值与该值相匹配时，条件才为真
 * - matchIfMissing 指定如果未找到属性时，默认匹配结果。默认为false，即属性不存在时不匹配
 */
@ConditionalOnProperty(prefix = "sys", name = "redis-template-config", havingValue = "true")
@Configuration
@Slf4j
public class RedisConfig<T> {

    /**
     * 配置RedisTemplate
     *
     * @param redisConnectionFactory Redis连接工厂
     * @return RedisTemplate实例
     */
    @Bean
    public RedisTemplate<String, T> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        log.info("RedisTemplateConfig init start ...");
        RedisTemplate<String, T> redisTemplate = new RedisTemplate<>();

        RedisSerializer<String> redisSerializer = new StringRedisSerializer();

        // 配置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        // 设置key的序列化规则
        redisTemplate.setKeySerializer(redisSerializer);
        // 设置hash key的序列化规则
        redisTemplate.setHashKeySerializer(redisSerializer);
        // =====================value采用Jackson序列化==========================================
        // value的序列化规则为Jackson
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer());
        // 设置hash value的序列化规则为Jackson
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer());

        return redisTemplate;
    }

    /**
     * 创建并配置一个Jackson2JsonRedisSerializer对象，用于序列化和反序列化Redis中的对象
     *
     * @return 配置好的Jackson2JsonRedisSerializer对象
     */
    private Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer() {
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);

        ObjectMapper objectMapper = new ObjectMapper();
        // 设置所有属性的可见性为ANY，确保在序列化和反序列化时，不受字段访问级别的限制
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 配置ObjectMapper，在反序列化时，对于JSON中存在但Java对象中不存在的属性，忽略这些未知属性
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 启用默认类型，允许反序列化时识别非final类型，便于处理多态情况
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);

        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        return jackson2JsonRedisSerializer;
    }

}
